package com.ics.atable.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ics.atable.chat.expection.ErrorCode;
import com.ics.atable.chat.expection.ThrowUtils;
import com.ics.atable.chat.model.entity.SemanticModel;
import com.ics.atable.chat.service.SemanticModelService;
import com.ics.atable.chat.mapper.SemanticModelMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 针对表【semantic_model(语义模型表)】的数据库操作Service实现
* @author haohongbin
*/
@Slf4j
@Service
public class SemanticModelServiceImpl extends ServiceImpl<SemanticModelMapper, SemanticModel>
    implements SemanticModelService{

    @Override
    public List<String> getSynonymsByNode(String content) {
        // 1. 校验 content 不能为空
        ThrowUtils.throwIf(StringUtils.isEmpty(content), ErrorCode.PARAMS_ERROR, "内容不能为空");

        // 2. 获取 content 对应的同义词
        List<String> synonyms = this.lambdaQuery()
                .eq(SemanticModel::getOriginName, content)
                .eq(SemanticModel::getStatus, 1)
                .select(SemanticModel::getSynonyms)
                .list()
                .stream()
                .map(SemanticModel::getSynonyms)
                .toList();

        // 3. recall_count + 1
        LambdaUpdateWrapper<SemanticModel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SemanticModel::getOriginName, content)
                .eq(SemanticModel::getStatus, 1)
                .setSql("recall_count = recall_count + 1");
        this.update(updateWrapper);

        // 4. 返回结果
        return synonyms;
    }

    @Override
    public boolean saveBySemanticMap(Map<String, List<String>> semanticMap) {
        // 1. 校验 semanticMap 不能为空
        ThrowUtils.throwIf(MapUtils.isEmpty(semanticMap), ErrorCode.PARAMS_ERROR, "内容不能为空");
        try {
            // 2. 遍历semanticMap构建语义模型列表
            List<SemanticModel> semanticModelList = new ArrayList<>();
            for (Map.Entry<String, List<String>> entry : semanticMap.entrySet()) {
                String originName = entry.getKey();
                List<String> synonyms = entry.getValue();
                for (String synonym : synonyms) {
                    SemanticModel semanticModel = new SemanticModel();
                    semanticModel.setOriginName(originName);
                    semanticModel.setSynonyms(synonym);
                    semanticModel.setStatus(1);
                    semanticModelList.add(semanticModel);
                }
            }

            // 3. 批量保存语义模型
            this.saveBatch(semanticModelList);
            return true;
        } catch (Exception e) {
            // 不影响后续操作
            log.info("保存语义模型失败：{}", e.getMessage());
            return false;
        }

    }

    @Override
    public void updateRecallCount(String content) {
        ThrowUtils.throwIf(StringUtils.isEmpty(content), ErrorCode.PARAMS_ERROR, "内容不能为空");
        try {
            LambdaUpdateWrapper<SemanticModel> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SemanticModel::getOriginName, content)
                    .eq(SemanticModel::getStatus, 1)
                    .setSql("recall_count = recall_count + 1");
            this.update(updateWrapper);
        } catch (Exception e) {
            log.info("更新召回次数失败：{}", e.getMessage());
        }
    }

}




